<template>
  <div>
    <ul>
      <li v-if="email">
        欢迎:{{ email }} /
        <a href="JavaScript:void(0)" @click="logout" color="green">退出登录</a>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 用户登录后的邮箱
      email: "",

      // 头像
      avtar: "",
    };
  },
  methods: {
    userinfo: function () {
      // 过去用户头像， 请求信息接口
      // 发起请求
      this.myaxios(this.weburl + "/userinfo/", "get").then((data) => {
        console.log(data);

        if (data.errcode == 0) {
          if (data.avtar.indexOf("http") == -1) {
            this.avtar = this.weburl + "/static/" + data.avtar;
          } else {
            this.avtar = data.avtar;
          }
        }
      });
    },
    // 退出登录方法
    logout: function () {
      // 删除localstorage
      localStorage.removeItem("token");
      localStorage.removeItem("email");
      this.email = "";

      // 跳转
      window.location.href = "/login";
    },
  },
  created() {
    // 判断用户是否登录
    if (localStorage.getItem("token") != null) {
      this.email = localStorage.getItem("email");
      this.userinfo();
    } else {
      this.email = "";
    }
  },
};
</script>


<style scoped>
@import url("../assets/style.css");
</style>

